<?php
class sekolah extends MY_Controller{
    function sekolah(){
        parent::MY_Controller();
        $this->data['title'] = "Master Data Sekolah";
        $this->load->model('m_sekolah');
        $this->load->model('m_tingkat_pendidikan');
    }
    
    var $data;
    var $limit = 10;
    
    function index(){
        $this->get_sekolah();
    }
    function get_sekolah($offset = 0){
        $this->data['content'] = "sekolah/v_sekolah";
        
        $uri_segment = 3;
        if($offset != 0){
          $offset = $this->uri->segment($uri_segment);  
        }
        $num_rows = $this->m_sekolah->getCountRows();
        $this->data['listSekolah'] = $this->m_sekolah->getListSekolah($offset,$this->limit);
        $config['base_url'] = site_url("sekolah/get_sekolah");
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        
        $this->render($this->data);
    }
    function add_sekolah_form($offset = 0){
        $this->data['content'] = "sekolah/v_sekolah";
        $this->data['subLink'] = "sekolah/add_sekolah_proses";
        $this->data['formTitle'] = "Form Tambah Data Sekolah";
        $this->data['formView'] = "sekolah/addSekolahForm";
        
        $uri_segment = 3;
        if($offset != 0){
          $offset = $this->uri->segment($uri_segment);  
        }
        $num_rows = $this->m_sekolah->getCountRows();
        $this->data['listSekolah'] = $this->m_sekolah->getListSekolah($offset,$this->limit);
        $this->data['listTingkatPendidikan'] = $this->m_tingkat_pendidikan->getListTingkatPendidikan();
        $config['base_url'] = site_url("sekolah/add_sekolah_form");
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        
        $this->render($this->data);
    }
    function edit_sekolah_form($id,$offset = 0){
        $this->data['content'] = "sekolah/v_sekolah";
        $this->data['subLink'] = "sekolah/edit_sekolah_proses";
        $this->data['formTitle'] = "Form Edit Data Sekolah";
        $this->data['formView'] = "sekolah/editSekolahForm";
        $this->data['dataInput'] = $this->m_sekolah->getSekolahById($id);
        
        $uri_segment = 4;
        if($offset != 0){
          $offset = $this->uri->segment($uri_segment);  
        }
        $num_rows = $this->m_sekolah->getCountRows();
        $this->data['listSekolah'] = $this->m_sekolah->getListSekolah($offset,$this->limit);
        $this->data['listTingkatPendidikan'] = $this->m_tingkat_pendidikan->getListTingkatPendidikan();
        $config['base_url'] = site_url("sekolah/edit_sekolah_form/".$id);
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        
        $this->render($this->data);
    }
    function add_sekolah_proses(){
        $data = array('nama' => $this->input->post('nama'),
            'alamat' => $this->input->post('alamat'),
            'penanggung_jawab' => $this->input->post('penanggung_jawab'),
            'no_telp' => $this->input->post('noTelp'),
            'id_tingkat_pendidikan' => $this->input->post('tingkatPendidikan'));
        $insert = $this->m_sekolah->addSekolah($data);
        if($insert == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("sekolah");
    }
    function edit_sekolah_proses(){
        $data = array('id' => $this->input->post('id'),'nama' => $this->input->post('nama'),
            'alamat' => $this->input->post('alamat'),
            'penanggung_jawab' => $this->input->post('penanggung_jawab'),
            'no_telp' => $this->input->post('noTelp'),
            'id_tingkat_pendidikan' => $this->input->post('tingkatPendidikan'));
        $update = $this->m_sekolah->editSekolah($data);
        if($update == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("sekolah");
    }
    function delete_sekolah($id){
        $delete = $this->m_sekolah->deleteSekolah($id);
        if($delete == TRUE){
            $this->session->set_flashdata("message",3);
        }else{
            $this->session->set_flashdata("message",4);
        }
        
        redirect("sekolah");
    }
}
?>
